using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Chimera2.GUI;
using Chimera2.Classes;
using Chimera2;
using CoreDC;

namespace Chimera2
{
    public partial class Front : Form
    {
        Main main;
        System.Resources.ResourceManager rm;
        Dictionary<Guid, TextBox> windows;

        #region Constructor

        public Front()
        {
            InitializeComponent();

            main = new Main(this);
            //string[] testar = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();

            rm = new System.Resources.ResourceManager("Chimera2.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());

            Datahandler dh = new Datahandler();
            main.settings = dh.LoadSettings();

            windows = new Dictionary<Guid, TextBox>();

            GenerateNodes();
            GenerateLogsConnectionLogs();
            GenerateLogsEditLogs();
        }

        #endregion

        #region hubContextMS

        private void addHubToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Hubs hubs = new Hubs(new Classes.Hub());
            hubs.passControl = new Hubs.PassControl(PassHubNew);
            hubs.Show();
        }

        private void editHubToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Hubs hubs = new Hubs(main.settings.hubs[(Guid)mainTreeView.SelectedNode.Tag]);

            hubs.passControl = new Hubs.PassControl(PassHubEdit);
            hubs.Show();
        }

        private void PassHubNew(object sender)
        {
            Classes.Hub hub = (Classes.Hub)sender;
            hub.Guid = System.Guid.NewGuid();

            if (!string.IsNullOrEmpty(hub.HubName))
            {
                main.settings.hubs.Add(hub.Guid, hub);
                AddNewNode(ParentNode.NodeHubs, hub.HubName, hub.HubAddress, hub.Guid, hubsContextMS);
                
                GenerateLogsConnectionLogs();
            }
        }

        private void PassHubEdit(object sender)
        {
            Classes.Hub hub = (Classes.Hub)sender;

            main.settings.hubs[hub.Guid] = hub;

            if (main.settings.hubs[hub.Guid].IsConnected)
            {
                main.clients[hub.Guid].Disconnect();
                main.clients[hub.Guid].Dispose();
            }
            UpdateNode(mainTreeView.SelectedNode, hub.HubName, hub.HubAddress, hub.Guid, true);

            GenerateLogsConnectionLogs();
        }

        private void copyHubToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Guid guid = (Guid)this.mainTreeView.SelectedNode.Tag;

            Hub hub = main.settings.hubs[guid].Clone();
            main.settings.hubs.Add(hub.Guid, hub);

            AddNewNode(ParentNode.NodeHubs, hub.HubName, hub.HubAddress, hub.Guid, hubsContextMS);
        }

        private void removeHubToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RemoveSelectedNode(sender, Types.Hub);
        }

        private void connectToHubToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (mainTreeView.SelectedNode.Level > 0)
            {
                TreeNode node = mainTreeView.SelectedNode;
                main.ConnectHub((Guid)node.Tag);
            }
        }

        private void disconnectFromHubToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (mainTreeView.SelectedNode.Level > 0)
            {
                TreeNode node = mainTreeView.SelectedNode;
                main.DisconnectHub((Guid)node.Tag);
            }
        }

        #endregion

        #region bulletinContextMS

        private void addBulletinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bulletins bulletins = new Bulletins(new Classes.Bulletin(), main.settings.hubs);

            bulletins.passControl = new Bulletins.PassControl(PassBulletinNew);
            bulletins.Show();
        }

        private void editBulletinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bulletins bulletins = new Bulletins();

            foreach (Bulletin bulletin in main.settings.bulletins.Values)
            {
                if (bulletin.Guid == (Guid)mainTreeView.SelectedNode.Tag)
                {
                    bulletins = new Bulletins(bulletin, main.settings.hubs);
                    break;
                }
            }

            bulletins.passControl = new Bulletins.PassControl(PassBulletinEdit);
            bulletins.Show();
        }

        private void PassBulletinNew(object sender)
        {
            Classes.Bulletin bulletin = (Classes.Bulletin)sender;
            bulletin.Guid = System.Guid.NewGuid();

            if (!string.IsNullOrEmpty(bulletin.BulletinName))
            {
                main.settings.bulletins.Add(bulletin.Guid, bulletin);

                AddNewNode(ParentNode.NodeBulletins, bulletin.BulletinName, bulletin.TextCmdName, bulletin.Guid, bulletinsContextMS);
            }

            GenerateLogsEditLogs();
        }

        private void PassBulletinEdit(object sender)
        {
            Bulletin bulletin = (Bulletin)sender;

            main.settings.bulletins[bulletin.Guid] = bulletin;

            mainTreeView.SelectedNode.Text = bulletin.BulletinName;

            GenerateLogsEditLogs();
        }

        private void copyBulletinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Guid guid = (Guid)this.mainTreeView.SelectedNode.Tag;

            Bulletin bulletin = main.settings.bulletins[guid].Clone();
            main.settings.bulletins.Add(bulletin.Guid, bulletin);

            AddNewNode(ParentNode.NodeBulletins, bulletin.BulletinName, bulletin.Guid, bulletinsContextMS);
        }

        private void removeBulletinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RemoveSelectedNode(sender, Types.Bulletin);
        }

        private void viewBulletinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: Open notepad or similar with bulletin displayed
        }

        private void viewEditlogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: Open notepad or similar with editlog displayed
        }

        #endregion

        #region adminContextMS

        private void addAdminToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Admins admins = new Admins(new Admin(), main.settings.hubs);
            admins.passControl = new Admins.PassControl(PassAdminNew);
            admins.Show();
        }

        private void editAdminToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Guid guid = (Guid)mainTreeView.SelectedNode.Tag;
            Admins admins = new Admins(main.settings.admins[guid], main.settings.hubs);

            admins.passControl = new Admins.PassControl(PassAdminEdit);
            admins.Show();
        }

        private void PassAdminNew(object sender)
        {
            Classes.Admin admin = (Classes.Admin)sender;
            admin.Guid = System.Guid.NewGuid();

            if (!string.IsNullOrEmpty(admin.Username))
            {
                main.settings.admins.Add(admin.Guid, admin);

                AddNewNode(ParentNode.NodeAdmins, admin.Username, admin.Guid, adminsContextMS);
            }
        }

        private void PassAdminEdit(object sender)
        {
            Classes.Admin admin = (Classes.Admin)sender;
            main.settings.admins[admin.Guid] = admin;

            mainTreeView.SelectedNode.Text = admin.Username;
        }

        private void copyAdminToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Guid guid = (Guid)this.mainTreeView.SelectedNode.Tag;

            Admin admin = main.settings.admins[guid].Clone();
            main.settings.admins.Add(admin.Guid, admin);

            AddNewNode(ParentNode.NodeAdmins, admin.Username, admin.Guid, adminsContextMS);
        }

        private void removeAdminToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RemoveSelectedNode(sender, Types.Admin);
        }

        #endregion
        
        #region editorContextMS

        private void addEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editors editors = new Editors(new Classes.Editor(), main.settings.hubs, main.settings.bulletins);
            editors.passControl = new Editors.PassControl(PassEditorNew);
            editors.Show();
        }

        private void editEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Guid guid = (Guid)mainTreeView.SelectedNode.Tag;
            Editor editor = main.settings.editors[guid];

            Editors editors = new Editors(editor, main.settings.hubs, main.settings.bulletins);

            editors.passControl = new Editors.PassControl(PassEditorEdit);
            editors.Show();
        }

        private void PassEditorNew(object sender)
        {
            Classes.Editor editor = (Classes.Editor)sender;
            editor.Guid = System.Guid.NewGuid();

            if (!string.IsNullOrEmpty(editor.UserName))
            {
                main.settings.editors.Add(editor.Guid, editor);

                AddNewNode(ParentNode.NodeEditors, editor.UserName, editor.Guid, editorsContextMS);
            }
        }

        private void PassEditorEdit(object sender)
        {
            Classes.Editor editor = (Classes.Editor)sender;
            main.settings.editors[editor.Guid] = editor;

            mainTreeView.SelectedNode.Text = editor.UserName;
        }

        private void copyEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Guid guid = (Guid)this.mainTreeView.SelectedNode.Tag;

            Editor editor = main.settings.editors[guid].Clone();
            main.settings.editors.Add(editor.Guid, editor);

            AddNewNode(ParentNode.NodeEditors, editor.UserName, editor.Guid, editorsContextMS);
        }

        private void removeEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RemoveSelectedNode(sender, Types.Editor);
        }

        #endregion

        #region mainMS

        // File

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GUI.Settings settings = new GUI.Settings(main.settings.botSettings);
            settings.passControl = new GUI.Settings.PassControl(PassSettings);

            settings.Show();
        }

        private void PassSettings(object sender)
        {
            BotSettings botSettings = ((BotSettings)sender);
            main.settings.botSettings = botSettings;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Datahandler dh = new Datahandler();
            dh.SaveSettings(main.settings);
            this.Close();
        }


        // Control

        private void allHubsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            main.ConnectAllHubs();
        }

        private void disconnectAllHubsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            main.DisconnectAllHubs();
        }

        private void cartoonsComicsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //CreateNewToolStripMenuItem("geekhubToolStripMenuItem", "Geekhub", this.oneByOneToolStripMenuItem, new EventHandler(this.connectToHub_Click));
        }

        /// <summary>
        /// Connect to specific hub
        /// </summary>
        void connectToHub_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;

            if (item.Checked)
            {
                main.DisconnectHub((Guid)item.Tag);
                item.Checked = false;
            }
            else
            {
                main.ConnectHub((Guid)item.Tag);
                item.Checked = true;
            }
        }


        // Logs

        /// <summary>
        /// View connection log for sender
        /// </summary>
        private void viewConnectionlogForHub_Click(object sender, EventArgs e)
        {
            // TODO: View connection log for hub x
        }

        /// <summary>
        /// View edit log for sender
        /// </summary>
        private void viewEditlogForBulletin_Click(object sender, EventArgs e)
        {
            // TODO: View edit log for bulletin x
        }

        /// <summary>
        /// View status log for bot
        /// </summary>
        private void viewStatuslogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: View status log for the bot
        }

        /// <summary>
        /// View admin log for bot
        /// </summary>
        private void viewAdminlogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: View admin log for the bot
        }

        // About

        /// <summary>
        /// Display Chimera2 information
        /// </summary>
        private void chimera2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Chimera2 version 1.0." + Environment.NewLine +
            "(C) Johan Pettersson, tenkan@gmail.com" + Environment.NewLine +
            "Licensed under the open source license GPL v2." + Environment.NewLine +
            "Homepage: http://peerfeeds.googlecode.com", "About Chimera2");
        }

        /// <summary>
        /// Display CoreDC information
        /// </summary>
        private void coreDCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Chimera2 uses the open source DC library CoreDC to connect to hubs." + Environment.NewLine +
            "Find out more about CoreDC at: http://coredc.googlecode.com", "About CoreDC");
        }


        private void mainTreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Action != TreeViewAction.Unknown)
            {
                if (e.Node.Nodes.Count > 0)
                {
                    /* Calls the CheckAllChildNodes method, passing in the current 
                    Checked value of the TreeNode whose checked state changed. */
                    this.CheckAllChildNodes(e.Node, e.Node.Checked);
                }
                else
                {
                    SetActiveStatus(e.Node.Parent.Name, (Guid)e.Node.Tag, e.Node.Checked);
                }
            }
        }

        private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                SetActiveStatus(node.Parent.Name, (Guid)node.Tag, node.Checked);
                if (node.Nodes.Count > 0)
                {
                    // If the current node has child nodes, call the CheckAllChildsNodes method recursively.
                    this.CheckAllChildNodes(node, nodeChecked);
                }
            }
        }

        private void SetActiveStatus(string parent, Guid guid, bool active)
        {
            switch (parent)
            {
                case "NodeHubs":
                    main.settings.hubs[guid].Active = active;
                    break;
                case "NodeBulletins":
                    main.settings.bulletins[guid].Active = active;
                    break;
                case "NodeAdmins":
                    main.settings.admins[guid].Active = active;
                    break;
                case "NodeEditors":
                    main.settings.editors[guid].Active = active;
                    break;
                default:
                    break;
            }
        }

        #endregion

        #region General functions for the form controls

        private void generalContextMS_Opening(object sender, CancelEventArgs e)
        {
            EnableDisableMenuChoices(sender);
        }

        private void EnableDisableMenuChoices(object sender)
        {
            if (mainTreeView.SelectedNode.Level == 0)
            {
                SetMenuChoices(sender, false);
            }
            else
            {
                SetMenuChoices(sender, true);
            }
        }

        private void SetMenuChoices(object sender, bool state)
        {
            ContextMenuStrip contextMs = (ContextMenuStrip)sender;

            for (int i = 1; i < contextMs.Items.Count; i++)
            {
                ToolStripItem item = contextMs.Items[i];
                item.Enabled = state;
            }

            TreeNode node = this.mainTreeView.SelectedNode;

            if (node.Parent != null && node.Parent.Name == "NodeHubs")
            {
                Guid guid = (Guid)node.Tag;

                if (main.settings.hubs[guid].IsConnected)
                {
                    contextMs.Items[5].Enabled = false;
                    contextMs.Items[6].Enabled = true;
                }
                else
                {
                    contextMs.Items[5].Enabled = true;
                    contextMs.Items[6].Enabled = false;
                }
            }
        }

        private void mainTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode node = this.mainTreeView.GetNodeAt(e.X, e.Y);

            if (node != null)
            {
                this.mainTreeView.SelectedNode = node;
            }

            if (node.Parent != null && node.Parent.Name == ParentNode.NodeHubs.ToString())
            {
                foreach (TextBox txt in windows.Values)
                {
                    txt.Visible = false;
                    if ((Guid)txt.Tag == (Guid)node.Tag)
                    {
                        txt.Visible = true;
                    }
                }
            }
        }

        private void Front_FormClosing(object sender, FormClosingEventArgs e)
        {
            Datahandler dh = new Datahandler();
            dh.SaveSettings(main.settings);
        }

        private void RemoveSelectedNode(object sender, Types type)
        {
            if (mainTreeView.SelectedNode.Level > 0)
            {
                TreeNode node = mainTreeView.SelectedNode;

                DialogResult result = MessageBox.Show("Are you sure you want to remove " + node.Text + "?", "Remove " + type.ToString(), MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                if (result == DialogResult.Yes)
                {
                    main.DeleteItem(type, (Guid)node.Tag);
                    mainTreeView.SelectedNode.Remove();

                    GenerateLogsConnectionLogs();
                    GenerateLogsEditLogs();
                }
            }
        }

        /// <summary>
        /// Creates a new MenuStripItem as a child to an existing one
        /// </summary>
        /// <param name="name">Name of the new MenuStripItem</param>
        /// <param name="text">Text for the new MenuStripItem</param>
        /// <param name="Guid">The Guid for the MenuStripItem</param>
        /// <param name="image">The image to display for the MenuStripItem</param>
        /// <param name="parent">The parent MenuStripItem</param>
        void CreateNewToolStripMenuItem(string name, string text, Guid Guid, Image image, ToolStripMenuItem parent, EventHandler eventHandler)
        {
            System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem;
            newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

            newToolStripMenuItem.Name = name;
            newToolStripMenuItem.Text = text;
            newToolStripMenuItem.Tag = Guid;
            newToolStripMenuItem.Image = image;

            newToolStripMenuItem.Click += eventHandler;

            parent.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            newToolStripMenuItem});
        }

        /// <summary>
        /// Creates a new MenuStripItem as a child to an existing one
        /// </summary>
        /// <param name="name">Name of the new MenuStripItem</param>
        /// <param name="text">Text for the new MenuStripItem</param>
        /// <param name="Guid">The Guid for the MenuStripItem</param> 
        /// <param name="parent">The parent MenuStripItem</param>
        void CreateNewToolStripMenuItem(string name, string text, Guid Guid, ToolStripMenuItem parent, EventHandler eventHandler)
        {
            System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem;
            newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

            newToolStripMenuItem.Name = name;
            newToolStripMenuItem.Text = text;
            newToolStripMenuItem.Tag = Guid;

            newToolStripMenuItem.Click += eventHandler;

            parent.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            newToolStripMenuItem});
        }

        void GenerateLogsConnectionLogs()       
        {
            this.viewConnectionlogToolStripMenuItem.DropDownItems.Clear();
            this.viewConnectionlogToolStripMenuItem.Enabled = (main.settings.hubs.Count > 0);

            foreach (Hub hub in main.settings.hubs.Values)
            {
                CreateNewToolStripMenuItem("connectionlogToolStripMenuItem", hub.HubName, hub.Guid, this.viewConnectionlogToolStripMenuItem, new EventHandler(this.viewConnectionlogForHub_Click));
            }
        }

        void GenerateLogsEditLogs()
        {
            this.viewEditlogForToolStripMenuItem.DropDownItems.Clear();
            this.viewEditlogForToolStripMenuItem.Enabled = (main.settings.bulletins.Count > 0);

            foreach (Bulletin bulletin in main.settings.bulletins.Values)
            {
                CreateNewToolStripMenuItem("editlogToolStripMenuItem", bulletin.BulletinName, bulletin.Guid, this.viewEditlogForToolStripMenuItem, new EventHandler(this.viewConnectionlogForHub_Click));
            }
        }
        
        #endregion

        #region General GUI functions

        public void UpdateStatusWindow(string text, Guid guid)
        {
            TextBox txt = windows[guid];
            txt.AppendText(DateTime.Now.ToLongTimeString() + " | " + text + Environment.NewLine);
        }

        private void AddNewNode(ParentNode parent, string text, Guid guid, ContextMenuStrip ms)
        {
            AddNewNode(parent, text, "", guid, ms);
        }

        private void AddNewNode(ParentNode parent, string text, string tooltip, Guid guid, ContextMenuStrip ms)
        {
            TreeNode node = new TreeNode();
            node.Text = text;
            node.Tag = guid;
            node.ToolTipText = tooltip;
            node.ContextMenuStrip = ms;
            node.ImageIndex = mainTreeView.Nodes[parent.ToString()].ImageIndex;
            node.SelectedImageIndex = mainTreeView.Nodes[parent.ToString()].SelectedImageIndex;
            node.Checked = true;

            mainTreeView.Nodes[parent.ToString()].Nodes.Add(node);

            if (parent == ParentNode.NodeHubs)
            {
                AddNewHubWindow(guid.ToString(), guid);
            }
        }

        private enum ParentNode
	    {
            NodeHubs, NodeBulletins, NodeAdmins, NodeEditors
    	}

        private void AddNewHubWindow(string name, Guid guid)
        {
            TextBox tb = new TextBox();

            tb.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            tb.Location = new System.Drawing.Point(246, 38);
            tb.Multiline = true;
            tb.Name = name;
            tb.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            tb.Size = new System.Drawing.Size(479, 403);
            tb.Visible = false;
            tb.Tag = guid;
            //tb.TabIndex = 8;

            this.Controls.Add(tb);
            this.windows.Add(guid, tb);
        }

        private void UpdateNode(TreeNode node, string text, Guid guid, bool active)
        {
            UpdateNode(node, text, "", guid, true);
        }

        private void UpdateNode(TreeNode node, string text, string tooltip, Guid guid, bool active)
        {
            node.Text = text;
            node.Tag = guid;
            node.Checked = active;
            node.ToolTipText = tooltip;
        }

        private void GenerateNodes()
        {
            foreach (Hub hub in main.settings.hubs.Values)
            {
                AddNewNode(ParentNode.NodeHubs, hub.HubName, hub.HubAddress, hub.Guid, hubsContextMS);
            }

            foreach (Bulletin bulletin in main.settings.bulletins.Values)
            {
                AddNewNode(ParentNode.NodeBulletins, bulletin.BulletinName, bulletin.Guid, bulletinsContextMS);
            }

            foreach (Admin admin in main.settings.admins.Values)
            {
                AddNewNode(ParentNode.NodeAdmins, admin.Username, admin.Guid, adminsContextMS);
            }

            foreach (Editor editor in main.settings.editors.Values)
            {
                AddNewNode(ParentNode.NodeEditors, editor.UserName, editor.Guid, editorsContextMS);
            }

        }
        
        #endregion

        #region TestFunctions

        private void button1_Click(object sender, EventArgs e)
        {
            Hub hub = new Hub();
            hub.HubAddress = "megaman.gotdns.com:411";
            hub.HubName = "Hub1";
            hub.HubBotName = "Kryten";
            hub.Guid = System.Guid.NewGuid();
            hub.OverrideBotName = true;
            hub.BotName = "Chimera1";
            hub.Active = true;

            Hub hub2 = new Hub();
            hub2.HubAddress = "megaman.gotdns.com:411";
            hub2.HubName = "Hub2";
            hub2.HubBotName = "Kryten2";
            hub2.Guid = System.Guid.NewGuid();
            hub2.OverrideBotName = false;
            hub2.BotName = "Chimera2";
            hub2.Active = true;

            main.settings.hubs.Add(hub.Guid, hub);
            main.settings.hubs.Add(hub2.Guid, hub2);

            AddNewNode(ParentNode.NodeHubs, hub.HubName, hub.HubAddress, hub.Guid, hubsContextMS);
            AddNewNode(ParentNode.NodeHubs, hub2.HubName, hub2.HubAddress, hub2.Guid, hubsContextMS);

            GenerateLogsConnectionLogs();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Bulletin bulletin = new Bulletin();
            bulletin.Active = true;
            bulletin.AccessFrom = new List<Guid>(main.settings.hubs.Keys);
            bulletin.AnnounceNew = true;
            bulletin.BulletinName = "Bulletin1";
            bulletin.GlobalAccess = false;
            bulletin.Guid = System.Guid.NewGuid();
            bulletin.OpsAreEditors = true;
            bulletin.RegularCanAdd = true;
            bulletin.RegularCanEdit = false;
            bulletin.RegularEditMinutes = 30;
            bulletin.TextCmdName = "Kryten";

            BulletinPost post = new BulletinPost();
            post.Created = DateTime.Now;
            post.Text = "This is a bulletinpost. Tihi";
            post.Updated = DateTime.Now;
            post.SortOrder = 1;
            foreach (Guid guid in main.settings.hubs.Keys)
            {
                post.UserHub = guid;
                break;
            }
            post.UserHubName = "hubname.. not sure where to get this from";
            post.UserName = "Todi";
            bulletin.Posts.Add(post);
            bulletin.Posts.Add(post);

            Bulletin bulletin2 = new Bulletin();
            bulletin2.Active = true;
            bulletin2.AccessFrom = new List<Guid>(main.settings.hubs.Keys);
            bulletin2.AnnounceNew = true;
            bulletin2.BulletinName = "Bulletin2";
            bulletin2.GlobalAccess = false;
            bulletin2.Guid = System.Guid.NewGuid();
            bulletin2.OpsAreEditors = true;
            bulletin2.RegularCanAdd = true;
            bulletin2.RegularCanEdit = false;
            bulletin2.RegularEditMinutes = 30;
            bulletin2.TextCmdName = "Kryten";
            bulletin2.Posts.Add(post);
            bulletin2.Posts.Add(post);

            main.settings.bulletins.Add(bulletin.Guid, bulletin);
            main.settings.bulletins.Add(bulletin2.Guid, bulletin2);

            AddNewNode(ParentNode.NodeBulletins, bulletin.BulletinName, bulletin.Guid, bulletinsContextMS);
            AddNewNode(ParentNode.NodeBulletins, bulletin2.BulletinName, bulletin2.Guid, bulletinsContextMS);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Admin admin = new Admin();
            admin.Active = true;
            admin.Username = "Todi1";
            admin.Guid = System.Guid.NewGuid();
            admin.CanAdminBulletins = true;
            admin.CanAdminHubs = true;
            admin.AccessFrom = new List<Guid>(main.settings.hubs.Keys);

            Admin admin2 = new Admin();
            admin2.Active = true;
            admin2.Username = "Todi2";
            admin2.Guid = System.Guid.NewGuid();
            admin2.CanAdminBulletins = false;
            admin2.CanAdminHubs = true;
            admin2.CanAdminEditors = true;
            admin2.AccessFrom = new List<Guid>(main.settings.hubs.Keys);

            main.settings.admins.Add(admin.Guid, admin);
            main.settings.admins.Add(admin2.Guid, admin2);

            AddNewNode(ParentNode.NodeAdmins, admin.Username, admin.Guid, adminsContextMS);
            AddNewNode(ParentNode.NodeAdmins, admin2.Username, admin2.Guid, adminsContextMS);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Editor editor = new Editor();
            editor.Active = true;
            editor.UserName = "Lightgirl1";
            editor.Guid = System.Guid.NewGuid();
            editor.CanEdit = true;
            editor.CanMove = true;
            editor.AccessFrom = new List<Guid>(main.settings.hubs.Keys);
            editor.AccessTo = new List<Guid>(main.settings.bulletins.Keys);
            editor.GlobalAccess = false;

            Editor editor2 = new Editor();
            editor2.Active = true;
            editor2.UserName = "Lightgirl2";
            editor2.Guid = System.Guid.NewGuid();
            editor2.CanEdit = true;
            editor2.CanMove = true;
            editor2.AccessFrom = new List<Guid>(main.settings.hubs.Keys);
            //editor2.AccessTo = new List<Guid>(main.settings.bulletins.Keys);
            editor2.GlobalAccess = true;

            main.settings.editors.Add(editor.Guid, editor);
            main.settings.editors.Add(editor2.Guid, editor2);

            AddNewNode(ParentNode.NodeEditors, editor.UserName, editor.Guid, editorsContextMS);
            AddNewNode(ParentNode.NodeEditors, editor2.UserName, editor2.Guid, editorsContextMS);
        }

        #endregion

    }
}